package com.AdvancedDemos.异常;

import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class 抛出异常捕获异常 {
    public static void main(String[] args) {
        try {
            test1();
        } catch (Exception e) {//Exception是所有异常的父类,被调函数抛出Exception,再在主函数中try catch Exception类异常,即可处理所有异常
            e.printStackTrace();//打印异常对象的信息,记录下来
            System.out.println("您当前的操作有问题!");
        }
    }

    public static void test1() throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = sdf.parse("2028-11-11 10:24:11");//不给秒,test1会抛出异常,再抛出给main函数;给秒,test1不产生异常,test2因为文件不存在,而抛出异常给test1,再抛出给main函数
        /*
        * java.text.ParseException: Unparseable date: "2028-11-11 10:24:"
	at java.base/java.text.DateFormat.parse(DateFormat.java:395)
	at com.AdvancedDemos.异常.异常的处理1.test1(异常的处理1.java:20)
	at com.AdvancedDemos.异常.异常的处理1.main(异常的处理1.java:11)
您当前的操作有问题!
        * */

        System.out.println(d);
        test2();
    }


    public static void test2() throws Exception {
        //读取文件
        InputStream is = new FileInputStream("D:/meinv.jpg");
    }
    /*
    * Sat Nov 11 10:24:11 CST 2028
您当前的操作有问题!
java.io.FileNotFoundException: D:\meinv.jpg (系统找不到指定的文件。)
	at java.base/java.io.FileInputStream.open0(Native Method)
	at java.base/java.io.FileInputStream.open(FileInputStream.java:213)
	at java.base/java.io.FileInputStream.<init>(FileInputStream.java:155)
	at java.base/java.io.FileInputStream.<init>(FileInputStream.java:110)
	at com.AdvancedDemos.异常.异常的处理1.test2(异常的处理1.java:36)
	at com.AdvancedDemos.异常.异常的处理1.test1(异常的处理1.java:30)
	at com.AdvancedDemos.异常.异常的处理1.main(异常的处理1.java:11)
    * */
}
